package com.luomo.wenyuan.modules.wenyuan.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.luomo.wenyuan.common.utils.PageUtils;
import com.luomo.wenyuan.common.utils.R;
import com.luomo.wenyuan.modules.wenyuan.dao.TBookDetailDao;
import com.luomo.wenyuan.modules.wenyuan.dao.TPutawayBookDao;
import com.luomo.wenyuan.modules.wenyuan.entity.*;
import com.luomo.wenyuan.modules.wenyuan.service.*;
import com.luomo.wenyuan.modules.wenyuan.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * @author
 * @email
 * @date 2020-10-19 15:58:40
 */
@RestController
@RequestMapping("generator/tputwayapplication")
public class TPutwayApplicationController {
    @Autowired
    private TPutwayApplicationService tPutwayApplicationService;

    @Autowired
    private TBookDetailDao tBookDetailDao;

    @Autowired
    private TPutawayBookService tPutawayBookService;

    @Autowired
    private TPutawayBookDao tPutawayBookDao;

    @Autowired
    private TBookService tBookService;

    @Autowired
    private TDeliveryService tDeliveryService;

    @Autowired
    private TBookDetailService tBookDetailService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tPutwayApplicationService.queryPage(params);
        List<TPutwayApplicationEntity> list = (List<TPutwayApplicationEntity>) page.getList();
        if (CollectionUtil.isNotEmpty(list)) {
            List<TPurchaseCheckVo> vos = new ArrayList<>();
            TPurchaseCheckVo vo;
            for (TPutwayApplicationEntity applicationEntity : list) {
                vo = new TPurchaseCheckVo();
                BeanUtil.copyProperties(applicationEntity, vo);
                List<TPutawayBook> purchaseBookEntities = tPutawayBookDao.getBookByPutawayId(applicationEntity.getId());
                List<TPurchaseBookVo> bookVos = new ArrayList<>();
                TPurchaseBookVo bookVo;
                for (TPutawayBook putawayBook : purchaseBookEntities) {
                    TBookEntity tBook = tBookService.getById(putawayBook.getBookId());
                    bookVo = new TPurchaseBookVo();
                    BeanUtil.copyProperties(tBook, bookVo);
                    bookVo.setNum(putawayBook.getNum());
                    bookVos.add(bookVo);
                }
                vo.setBookList(bookVos);
                vos.add(vo);
            }
            page.setList(vos);
        }
        return R.ok().put("page", page);
    }


    @RequestMapping("/check/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R check(@PathVariable("id") int id) {
        TPutwayApplicationEntity applicationEntity = tPutwayApplicationService.getById(id);
        if (applicationEntity != null) {
            applicationEntity.setStatus(2);
        }
        tPutwayApplicationService.updateById(applicationEntity);
        TDeliveryEntity deliveryEntity = new TDeliveryEntity();
        deliveryEntity.setOperateDate(new Date());
        deliveryEntity.setPutawayId(id);
        deliveryEntity.setStatus(1);
        tDeliveryService.save(deliveryEntity);
        return R.ok();
    }

    @RequestMapping("/up/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R up(@PathVariable("id") Integer id, @RequestBody List<TUpBookDetailVo> bookDetailVos) {
        UpdateWrapper<TPutwayApplicationEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("status",4);
        Date now = new Date();
        tPutwayApplicationService.update(updateWrapper);
        for (TUpBookDetailVo bookDetailVo : bookDetailVos) {
            Integer bookDetailId = bookDetailVo.getDetailId();
            String upLocation = bookDetailVo.getUpLocation();
            UpdateWrapper<TBookDetailEntity> detailUpdate = new UpdateWrapper<>();
            detailUpdate.eq("id",bookDetailId);
            detailUpdate.set("status",3);
            detailUpdate.set("up_location",upLocation);
            detailUpdate.set("up_date",now);
            tBookDetailService.update(detailUpdate);
            UpdateWrapper<TPutawayBookEntity> putawayUpdate = new UpdateWrapper<>();
            putawayUpdate.eq("putaway_id",id);
            putawayUpdate.eq("book_detail_id",bookDetailId);
            putawayUpdate.set("up_location",upLocation);
            putawayUpdate.set("up_date",now);
            tPutawayBookService.update(putawayUpdate);
        }
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        TPutwayApplicationEntity tPutwayApplication = tPutwayApplicationService.getById(id);

        return R.ok().put("tPutwayApplication", tPutwayApplication);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public R save(@RequestBody TPutawayApplicationVo vo) {
        TPutwayApplicationEntity applicationEntity = new TPutwayApplicationEntity();
        applicationEntity.setApplicant(vo.getApplicant());
        applicationEntity.setNote(vo.getNote());
        applicationEntity.setApplyDate(new Date());
        applicationEntity.setStatus(1);
        tPutwayApplicationService.save(applicationEntity);
        List<TPutawayBookVo> bookList = vo.getBookList();
        Integer applicationEntityId = applicationEntity.getId();
        List<TPutawayBookEntity> tPutawayBookEntities = new ArrayList<>();
        TPutawayBookEntity tPutawayBookEntity;
        for (TPutawayBookVo bookEntity : bookList) {
            Integer bookId = bookEntity.getBookId();
            Integer num = bookEntity.getNum();
            for (Integer i = 0; i < num; i++) {
                TBookDetailEntity bookDetail = tBookDetailDao.getOneByBookIdAndStatus(bookId, 1);
                bookDetail.setFlag(1);
                tBookDetailDao.updateById(bookDetail);
                tPutawayBookEntity = new TPutawayBookEntity();
                tPutawayBookEntity.setBookDetailId(bookDetail.getId());
                tPutawayBookEntity.setPutawayId(applicationEntityId);
                tPutawayBookEntities.add(tPutawayBookEntity);
            }
        }
        tPutawayBookService.saveBatch(tPutawayBookEntities);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TPutwayApplicationEntity tPutwayApplication) {
        tPutwayApplicationService.updateById(tPutwayApplication);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        tPutwayApplicationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
